package com.smile.frame.mq.rabbit.mq.listener.voucher;


import com.rabbitmq.client.Channel;
import com.smile.frame.config.rabbit.component.BasicHandler;
import com.smile.frame.config.rabbit.voucher.VoucherRabbitConfig;
import com.smile.frame.config.rabbit.voucher.message.VoucherCreateMessage;
import com.smile.frame.config.rabbit.voucher.message.VoucherExpireRefundDelayMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;


/**
 * 凭证相关消息监听
 *
 * @author LGC
 */
@Component
@Slf4j
public class VoucherListener extends BasicHandler {


    /**
     * 凭证生成
     *
     * @param payload
     * @param headers
     * @param channel
     * @param message
     * @throws IOException
     *
     * prefetch: 1   预抓取1个消息放到自己的LinkedBlockingQueue进行消费
     * concurrency = "1" 覆盖全局配置concurrency: 5，开启1个线程去处理消息
     */
    @RabbitListener(queues = VoucherRabbitConfig.VOUCHER_CREATE_QUEUE, concurrency = "1")
    public void voucherCreateHandler(@Payload VoucherCreateMessage payload, @Headers Map<String, Object> headers, Channel channel, Message message) throws IOException {
        try {
            log.info("{}:channelNumber：{}", VoucherRabbitConfig.VOUCHER_CREATE_QUEUE, channel.getChannelNumber());
            log.info("{}:payload：{}", VoucherRabbitConfig.VOUCHER_CREATE_QUEUE, payload);
            log.info("{}:headers：{}", VoucherRabbitConfig.VOUCHER_CREATE_QUEUE, headers);
            //TODO 具体业务
            log.info("生成凭证...");
            super.basicAck(channel, message);
            log.info("生成凭证完成");
        } catch (Exception e) {
            super.exceptionHandler(channel, message, e);
        }
    }


    /**
     * 凭证过期可退
     *
     * @param payload
     * @param headers
     * @param channel
     * @param message
     * @throws IOException
     */
    @RabbitListener(queues = VoucherRabbitConfig.VOUCHER_EXPIRE_REFUND_DELAY_QUEUE)
    public void voucherExpireRefundDelayHandler(@Payload VoucherExpireRefundDelayMessage payload, @Headers Map<String, Object> headers, Channel channel, Message message) throws IOException {
        try {
            log.info("{}:channelNumber：{}", VoucherRabbitConfig.VOUCHER_CREATE_QUEUE, channel.getChannelNumber());
            log.info("{}:payload：{}", VoucherRabbitConfig.VOUCHER_EXPIRE_REFUND_DELAY_QUEUE, payload);
            log.info("{}:headers：{}", VoucherRabbitConfig.VOUCHER_EXPIRE_REFUND_DELAY_QUEUE, headers);
            //TODO 具体业务
            log.info("凭证过期可退...");
            super.basicAck(channel, message);
            log.info("凭证过期可退完成");
        } catch (Exception e) {
            super.exceptionHandler(channel, message, e);
        }
    }


}